home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / VertexShader / vertexshader.frm < prev    next >
Text File  |  2001-10-08  |  18KB  |  555 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Vertex Blend"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5640
  8.    Icon            =   "vertexshader.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   299
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   376
  13.    StartUpPosition =   3  'Windows Default
  14. End
  15. Attribute VB_Name = "Form1"
  16. Attribute VB_GlobalNameSpace = False
  17. Attribute VB_Creatable = False
  18. Attribute VB_PredeclaredId = True
  19. Attribute VB_Exposed = False
  20.  
  21. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  22. '
  23. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  24. '
  25. '  File:       VertexShader.frm
  26. '  Content:    Example code showing how to use vertex shaders in D3D.
  27. '
  28. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  29.  
  30. Option Explicit
  31.  
  32.  
  33. ' Scene
  34. Dim m_VB As Direct3DVertexBuffer8
  35. Dim m_IB As Direct3DIndexBuffer8
  36. Dim m_NumVertices As Long
  37. Dim m_NumIndices As Long
  38. Dim m_Shader As Long
  39. Dim m_Size As Long
  40.  
  41. ' Transforms
  42. Dim m_matPosition As D3DMATRIX
  43. Dim m_matView As D3DMATRIX
  44. Dim m_matProj As D3DMATRIX
  45.  
  46. 'Navigation
  47. Dim m_bKey(256) As Boolean
  48. Dim m_fSpeed As Single
  49. Dim m_fAngularSpeed As Single
  50.  
  51. Dim m_vVelocity As D3DVECTOR
  52. Dim m_vAngularVelocity As D3DVECTOR
  53.  
  54. 'Shader
  55. Dim m_Decl(3) As Long
  56. Dim m_ShaderArray() As Long
  57.  
  58. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  59. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  60.  
  61. '-----------------------------------------------------------------------------
  62. ' Name: Form_Load()
  63. ' Desc:
  64. '-----------------------------------------------------------------------------
  65. Private Sub Form_Load()
  66.     Me.Show
  67.     DoEvents
  68.     
  69.     'setup defaults
  70.     Init
  71.     
  72.     ' Initialize D3D
  73.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  74.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  75.     ' If all fail it will display a message box indicating so.
  76.     '
  77.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  78.     If Not (m_bInit) Then End
  79.     
  80.     
  81.     ' Create new D3D vertexbuffer objects and vertex shader
  82.     InitDeviceObjects
  83.     
  84.     ' Sets the state for those objects and the current D3D device
  85.     RestoreDeviceObjects
  86.     
  87.     ' Start our timer
  88.     DXUtil_Timer TIMER_start
  89.     
  90.     ' Run the simulation forever
  91.     ' See Form_Keydown for exit processing
  92.     Do While True
  93.     
  94.         ' Increment the simulation
  95.         FrameMove
  96.         
  97.         ' Render one image of the simulation
  98.         If Render Then
  99.         
  100.             ' Present the image to the screen
  101.             D3DUtil_PresentAll g_focushwnd
  102.         End If
  103.         
  104.         ' Allow for events to get processed
  105.         DoEvents
  106.         
  107.     Loop
  108.     
  109. End Sub
  110.  
  111.  
  112. '-----------------------------------------------------------------------------
  113. ' Name: Form_Unload()
  114. ' Desc:
  115. '-----------------------------------------------------------------------------
  116. Private Sub Form_Unload(Cancel As Integer)
  117.     DeleteDeviceObjects
  118.     End
  119. End Sub
  120.  
  121.  
  122.  
  123. '-----------------------------------------------------------------------------
  124. ' Name: Init()
  125. ' Desc: Sets attributes for the app.
  126. '-----------------------------------------------------------------------------
  127. Sub Init()
  128.  
  129.  
  130.     Me.Caption = "VertexShader"
  131.     
  132.     Set m_IB = Nothing
  133.     Set m_VB = Nothing
  134.     m_Size = 32
  135.     m_NumIndices = (m_Size - 1) * (m_Size - 1) * 6
  136.     m_NumVertices = m_Size * m_Size
  137.     m_Shader = 0
  138.     
  139.     m_fSpeed = 5
  140.     m_fAngularSpeed = 1
  141.  
  142.     m_vVelocity = vec3(0, 0, 0)
  143.     m_vAngularVelocity = vec3(0, 0, 0)
  144.     
  145.     
  146.     ' Setup the view matrix
  147.     Dim veye As D3DVECTOR, vat As D3DVECTOR, vUp As D3DVECTOR
  148.     veye = vec3(2, 3, 3)
  149.     vat = vec3(0, 0, 0)
  150.     vUp = vec3(0, 1, 0)
  151.     D3DXMatrixLookAtRH m_matView, veye, vat, vUp
  152.  
  153.     ' Set the position matrix
  154.     Dim det As Single
  155.     D3DXMatrixInverse m_matPosition, det, m_matView
  156.  
  157. End Sub
  158.  
  159.  
  160.  
  161. '-----------------------------------------------------------------------------
  162. ' Name: FrameMove()
  163. ' Desc: Called once per frame, the call is the entry point for animating
  164. '       the scene.
  165. '-----------------------------------------------------------------------------
  166. Sub FrameMove()
  167.     Dim fSecsPerFrame As Single
  168.     Dim fTime As Single
  169.     Dim det As Single
  170.     
  171.     fSecsPerFrame = DXUtil_Timer(TIMER_GETELLAPSEDTIME)
  172.     fTime = DXUtil_Timer(TIMER_GETAPPTIME)
  173.  
  174.     ' Process keyboard input
  175.     Dim vT As D3DVECTOR, vR As D3DVECTOR
  176.     
  177.     vT = vec3(0, 0, 0)
  178.     vR = vec3(0, 0, 0)
  179.  
  180.     
  181.     If (m_bKey(vbKeyA) Or m_bKey(vbKeyNumpad1) Or m_bKey(vbKeyLeft)) Then vT.x = vT.x - 1  ' Slide Left
  182.     If (m_bKey(vbKeyD) Or m_bKey(vbKeyNumpad3) Or m_bKey(vbKeyRight)) Then vT.x = vT.x + 1 ' Slide Right
  183.     If (m_bKey(vbKeyDown)) Then vT.y = vT.y - 1                                      ' Slide Down
  184.     If (m_bKey(vbKeyUp)) Then vT.y = vT.y + 1                                        ' Slide Up
  185.     If (m_bKey(vbKeyW)) Then vT.z = vT.z - 2                                         ' Move Forward
  186.     If (m_bKey(vbKeyS)) Then vT.z = vT.z + 2                                         ' Move Backward
  187.     If (m_bKey(vbKeyNumpad8)) Then vR.x = vR.x - 1                                   ' Pitch Down
  188.     If (m_bKey(vbKeyNumpad2)) Then vR.x = vR.x + 1                                   ' Pitch Up
  189.     If (m_bKey(vbKeyE) Or m_bKey(vbKeyNumpad6)) Then vR.y = vR.y - 1                 ' Turn Right
  190.     If (m_bKey(vbKeyQ) Or m_bKey(vbKeyNumpad4)) Then vR.y = vR.y + 1                 ' Turn Left
  191.     If (m_bKey(vbKeyNumpad9)) Then vR.z = vR.z - 2                                   ' Roll CW
  192.     If (m_bKey(vbKeyNumpad7)) Then vR.z = vR.z + 2                                   ' Roll CCW
  193.  
  194.     m_vVelocity.x = m_vVelocity.x * 0.9 + vT.x * 0.1
  195.     m_vVelocity.y = m_vVelocity.y * 0.9 + vT.y * 0.1
  196.     m_vVelocity.z = m_vVelocity.z * 0.9 + vT.z * 0.1
  197.     m_vAngularVelocity.x = m_vAngularVelocity.x * 0.9 + vR.x * 0.1
  198.     m_vAngularVelocity.y = m_vAngularVelocity.x * 0.9 + vR.y * 0.1
  199.     m_vAngularVelocity.z = m_vAngularVelocity.x * 0.9 + vR.z * 0.1
  200.  
  201.     ' Update position and view matricies
  202.     Dim matT As D3DMATRIX, matR As D3DMATRIX, qR As D3DQUATERNION
  203.     
  204.     D3DXVec3Scale vT, m_vVelocity, fSecsPerFrame * m_fSpeed
  205.     D3DXVec3Scale vR, m_vAngularVelocity, fSecsPerFrame * m_fAngularSpeed
  206.     
  207.  
  208.     D3DXMatrixTranslation matT, vT.x, vT.y, vT.z
  209.     D3DXMatrixMultiply m_matPosition, matT, m_matPosition
  210.  
  211.     D3DXQuaternionRotationYawPitchRoll qR, vR.y, vR.x, vR.z
  212.     D3DXMatrixRotationQuaternion matR, qR
  213.  
  214.     D3DXMatrixMultiply m_matPosition, matR, m_matPosition
  215.     D3DXMatrixInverse m_matView, det, m_matPosition
  216.     g_dev.SetTransform D3DTS_VIEW, m_matView
  217.  
  218.     ' Set up the vertex shader constants
  219.     
  220.     Dim mat As D3DMATRIX
  221.     Dim vA As D3DVECTOR4, vD As D3DVECTOR4
  222.     Dim vSin As D3DVECTOR4, vCos As D3DVECTOR4
  223.     
  224.     D3DXMatrixMultiply mat, m_matView, m_matProj
  225.     D3DXMatrixTranspose mat, mat
  226.  
  227.     vA = vec4(Sin(fTime) * 15, 0, 0.5, 1)
  228.     vD = vec4(g_pi, 1 / (2 * g_pi), 2 * g_pi, 0.05)
  229.  
  230.     ' Taylor series coefficients for sin and cos
  231.     vSin = vec4(1, -1 / 6, 1 / 120, -1 / 5040)
  232.     vCos = vec4(1, -1 / 2, 1 / 24, -1 / 720)
  233.  
  234.     g_dev.SetVertexShaderConstant 0, mat, 4
  235.     g_dev.SetVertexShaderConstant 4, vA, 1
  236.     g_dev.SetVertexShaderConstant 7, vD, 1
  237.     g_dev.SetVertexShaderConstant 10, vSin, 1
  238.     g_dev.SetVertexShaderConstant 11, vCos, 1
  239.    
  240.  
  241. End Sub
  242.  
  243.  
  244.  
  245. '-----------------------------------------------------------------------------
  246. ' Name: Render()
  247. ' Desc: Called once per frame, the call is the entry point for 3d
  248. '       rendering. This function sets up render states, clears the
  249. '       viewport, and renders the scene.
  250. '-----------------------------------------------------------------------------
  251. Function Render() As Boolean
  252.     Dim v2 As D3DVECTOR2
  253.     Dim hr As Long
  254.     
  255.     Render = False
  256.     'See what state the device is in.
  257.     hr = g_dev.TestCooperativeLevel
  258.     If hr = D3DERR_DEVICENOTRESET Then
  259.         g_dev.Reset g_d3dpp
  260.         RestoreDeviceObjects
  261.     End If
  262.     
  263.     'dont bother rendering if we are not ready yet
  264.     If hr <> 0 Then Exit Function
  265.     Render = True
  266.     
  267.     'Clear the scene
  268.     D3DUtil_ClearAll &HFF&
  269.     
  270.     With g_dev
  271.     
  272.         ' Begin the scene
  273.         .BeginScene
  274.         
  275.         .SetVertexShader m_Shader
  276.         .SetStreamSource 0, m_VB, Len(v2)
  277.         .SetIndices m_IB, 0
  278.         
  279.         .DrawIndexedPrimitive D3DPT_TRIANGLELIST, 0, m_NumVertices, _
  280.                                             0, m_NumIndices / 3
  281.  
  282.         ' End the scene.
  283.         .EndScene
  284.     
  285.     End With
  286.  
  287. End Function
  288.  
  289. '-----------------------------------------------------------------------------
  290. ' Name: RestoreDeviceObjects()
  291. ' Desc: Initialize scene objects.
  292. '-----------------------------------------------------------------------------
  293. Sub InitDeviceObjects()
  294.  
  295.     Dim Indices() As Integer    'Integer are 4 bytes wide in VB
  296.     Dim Vertices() As D3DVECTOR2
  297.     Dim v As D3DVECTOR2, x As Integer, y As Integer, i As Integer
  298.     
  299.             
  300.     ' Fill in our index array with triangles indices to make a grid
  301.     ReDim Indices(m_NumIndices)
  302.     For y = 1 To m_Size - 1
  303.         For x = 1 To m_Size - 1
  304.             Indices(i) = (y - 1) * m_Size + (x - 1): i = i + 1
  305.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  306.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  307.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  308.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  309.             Indices(i) = (y - 0) * m_Size + (x - 0): i = i + 1
  310.         Next
  311.     Next
  312.     
  313.     ' Create index buffer and copy the VB array into it
  314.     Set m_IB = g_dev.CreateIndexBuffer(m_NumIndices * 2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED)
  315.     D3DIndexBuffer8SetData m_IB, 0, m_NumIndices * 2, 0, Indices(0)
  316.  
  317.     i = 0
  318.         
  319.     'Fill our vertex array with the coordinates of our grid
  320.     ReDim Vertices(m_NumVertices)
  321.     For y = 0 To m_Size - 1
  322.         For x = 0 To m_Size - 1
  323.             Vertices(i) = vec2(((CSng(x) / CSng(m_Size - 1)) - 0.5) * g_pi, _
  324.                             ((CSng(y) / CSng(m_Size - 1)) - 0.5) * g_pi)
  325.                            
  326.             i = i + 1
  327.         Next
  328.     Next
  329.     
  330.  
  331.     ' Create a vertex buffer and copy our vertex array into it
  332.     Set m_VB = g_dev.CreateVertexBuffer(m_NumVertices * Len(v), 0, 0, D3DPOOL_MANAGED)
  333.     D3DVertexBuffer8SetData m_VB, 0, m_NumVertices * Len(v), 0, Vertices(0)
  334.     
  335.     
  336.     
  337.     ' Create vertex shader
  338.     Dim strVertexShaderPath As String
  339.     Dim VShaderCode As D3DXBuffer
  340.     
  341.     
  342.     
  343.     m_Decl(0) = D3DVSD_STREAM(0)
  344.     m_Decl(1) = D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT2)
  345.     m_Decl(2) = D3DVSD_END()
  346.         
  347.  
  348.     ' Find the vertex shader file
  349.     strVertexShaderPath = FindMediaDir("ripple.vsh") + "ripple.vsh"
  350.     
  351.     'Assemble the vertex shader from the file
  352.     Set VShaderCode = g_d3dx.AssembleShaderFromFile(strVertexShaderPath, 0, "", Nothing)
  353.             
  354.     'Move VShader code into an array
  355.     ReDim m_ShaderArray(VShaderCode.GetBufferSize() / 4)
  356.     g_d3dx.BufferGetData VShaderCode, 0, 1, VShaderCode.GetBufferSize(), m_ShaderArray(0)
  357.  
  358.     Set VShaderCode = Nothing
  359.  
  360.    
  361. End Sub
  362.  
  363.  
  364.  
  365. '-----------------------------------------------------------------------------
  366. ' Name: RestoreDeviceObjects()
  367. ' Desc: Initialize scene objects.
  368. '-----------------------------------------------------------------------------
  369. Sub RestoreDeviceObjects()
  370.  
  371.     Dim bufferdesc As D3DSURFACE_DESC
  372.     g_dev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO).GetDesc bufferdesc
  373.     
  374.     ' Set up right handed projection matrix
  375.     Dim fAspectRatio As Single
  376.     fAspectRatio = bufferdesc.width / bufferdesc.height
  377.     D3DXMatrixPerspectiveFovRH m_matProj, 60 * g_pi / 180, fAspectRatio, 0.1, 100
  378.     g_dev.SetTransform D3DTS_PROJECTION, m_matProj
  379.  
  380.     ' Setup render states
  381.     g_dev.SetRenderState D3DRS_LIGHTING, 0 'FALSE
  382.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  383.     
  384.      
  385.     ' Create the vertex shader
  386.     ' NOTE returns value in m_Shader
  387.     g_dev.CreateVertexShader m_Decl(0), m_ShaderArray(0), m_Shader, 0
  388.     
  389.  
  390. End Sub
  391.  
  392.  
  393.  
  394. '-----------------------------------------------------------------------------
  395. ' Name: InvalidateDeviceObjects()
  396. ' Desc:
  397. '-----------------------------------------------------------------------------
  398. Sub InvalidateDeviceObjects()
  399.     On Local Error Resume Next
  400.     g_dev.DeleteVertexShader m_Shader
  401. End Sub
  402.  
  403.  
  404.  
  405. '-----------------------------------------------------------------------------
  406. ' Name: DeleteDeviceObjects()
  407. ' Desc: Called when the app is exitting, or the device is being changed,
  408. '       this function deletes any device dependant objects.
  409. '-----------------------------------------------------------------------------
  410. Sub DeleteDeviceObjects()
  411.     Set m_IB = Nothing
  412.     Set m_VB = Nothing
  413.     InvalidateDeviceObjects
  414.     m_bInit = False
  415. End Sub
  416.  
  417.  
  418. '-----------------------------------------------------------------------------
  419. ' Name: FinalCleanup()
  420. ' Desc: Called before the app exits, this function gives the app the chance
  421. '       to cleanup after itself.
  422. '-----------------------------------------------------------------------------
  423. Sub FinalCleanup()
  424.  
  425. End Sub
  426.  
  427.  
  428. '-----------------------------------------------------------------------------
  429. ' Name: ConfirmDevice()
  430. ' Desc: Called during device intialization, this code checks the device
  431. '       for some minimum set of capabilities
  432. '-----------------------------------------------------------------------------
  433. Function VerifyDevice(Behavior As Long, format As CONST_D3DFORMAT) As Boolean
  434.  
  435.     If (Behavior <> D3DCREATE_SOFTWARE_VERTEXPROCESSING) Then
  436.         If (g_d3dCaps.VertexShaderVersion < D3DVS_VERSION(1, 0)) Then Exit Function
  437.     End If
  438.     VerifyDevice = True
  439. End Function
  440.  
  441.  
  442.  
  443. '-----------------------------------------------------------------------------
  444. ' Name: Form_KeyDown()
  445. ' Desc: Process key messages for exit and change device
  446. '-----------------------------------------------------------------------------
  447. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  448.      Dim hr As Long
  449.      
  450.      m_bKey(KeyCode) = True
  451.     
  452.      Select Case KeyCode
  453.         
  454.         Case vbKeyEscape
  455.             Unload Me
  456.             
  457.         Case vbKeyF2
  458.                 
  459.             ' Pause the timer
  460.             DXUtil_Timer TIMER_STOP
  461.             
  462.             ' Bring up the device selection dialog
  463.             ' we pass in the form so the selection process
  464.             ' can make calls into InitDeviceObjects
  465.             ' and RestoreDeviceObjects
  466.             frmSelectDevice.SelectDevice Me
  467.             
  468.             ' Restart the timer
  469.             DXUtil_Timer TIMER_start
  470.             
  471.         Case vbKeyReturn
  472.         
  473.             ' Check for Alt-Enter if not pressed exit
  474.             If Shift <> 4 Then Exit Sub
  475.             
  476.             ' If we are windowed go fullscreen
  477.             ' If we are fullscreen returned to windowed
  478.             If g_d3dpp.Windowed Then
  479.                  hr = D3DUtil_ResetFullscreen
  480.             Else
  481.                  hr = D3DUtil_ResetWindowed
  482.             End If
  483.                                                           
  484.             If hr = D3DERR_DEVICELOST Then
  485.                 
  486.                 DeleteDeviceObjects
  487.                 
  488.                 m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  489.                 If Not (m_bInit) Then End
  490.                 
  491.                 InitDeviceObjects
  492.             End If
  493.             
  494.             ' Call Restore after ever mode change
  495.             ' because calling reset looses state that needs to
  496.             ' be reinitialized
  497.             RestoreDeviceObjects
  498.            
  499.     End Select
  500. End Sub
  501.  
  502.  
  503. '-----------------------------------------------------------------------------
  504. ' Name: Form_Resize()
  505. ' Desc: hadle resizing of the D3D backbuffer
  506. '-----------------------------------------------------------------------------
  507. Private Sub Form_Resize()
  508.  
  509.     ' If D3D is not initialized then exit
  510.     If Not m_bInit Then Exit Sub
  511.     
  512.     ' If we are in a minimized state stop the timer and exit
  513.     If Me.WindowState = vbMinimized Then
  514.         DXUtil_Timer TIMER_STOP
  515.         m_bMinimized = True
  516.         Exit Sub
  517.         
  518.     ' If we just went from a minimized state to maximized
  519.     ' restart the timer
  520.     Else
  521.         If m_bMinimized = True Then
  522.             DXUtil_Timer TIMER_start
  523.             m_bMinimized = False
  524.         End If
  525.     End If
  526.     
  527.     ' Dont let the window get too small
  528.     If Me.ScaleWidth < 10 Then
  529.         Me.width = Screen.TwipsPerPixelX * 10
  530.         Exit Sub
  531.     End If
  532.     
  533.     If Me.ScaleHeight < 10 Then
  534.         Me.height = Screen.TwipsPerPixelY * 10
  535.         Exit Sub
  536.     End If
  537.  
  538.         
  539.     'reset and resize our D3D backbuffer to the size of the window
  540.     D3DUtil_ResizeWindowed Me.hwnd
  541.     
  542.     'All state get losts after a reset so we need to reinitialze it here
  543.     RestoreDeviceObjects
  544.     
  545. End Sub
  546.  
  547.  
  548. '-----------------------------------------------------------------------------
  549. ' Name: Picture1_KeyUp
  550. ' Desc:
  551. '-----------------------------------------------------------------------------
  552. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  553.     m_bKey(KeyCode) = False
  554. End Sub
  555.